package com.alibaba.citrus.ots.common.util;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: yuhw
 * @date: 2021年09月22日 16:19
 **/
public class JsonUtil {
    private static final Log log = Log.getLogger(JsonUtil.class);

    public static final String ORDER_CENTER_TAG = "OrderCenter";

    public static <T> T convert(Object obj, Class<T> clazz) {
        return JSONObject.parseObject(JSONObject.toJSONString(obj), clazz);
    }

    public static <T> List<T> convertList(Object obj, Class<T> clazz) {
        return JSONObject.parseArray(JSONObject.toJSONString(obj), clazz);
    }

    /**
     * 判断是否为订单中心订单
     */
    public static boolean checkOrderCenterTag(Map<String, String> features) {
        if (MapUtils.isEmpty(features)) {
            return false;
        }
        try {
            String os = features.get("os");
            if (StringUtils.isBlank(os) || !ORDER_CENTER_TAG.equals(os)) {
                return false;
            }
        } catch (JSONException e) {
            log.error("JsonUtil#checkOrderCenterTag error", e);
            return false;
        }
        return true;
    }

    /**
     * 判断是否为订单中心订单
     */
    public static boolean checkOrderCenterTag(String features) {
        if (StringUtils.isBlank(features)) {
            return false;
        }
        try {
            JSONObject json = JSON.parseObject(features);
            String os = json.getString("os");
            if (StringUtils.isBlank(os) || !ORDER_CENTER_TAG.equals(os)) {
                return false;
            }
        } catch (JSONException e) {
            log.error("JsonUtil#checkOrderCenterTag error", e);
            return false;
        }
        return true;
    }
}
